package com.probiz.estore.system.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.probiz.estore.common.model.system.AppRole;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.system.dao.AppRoleDao;

/**
 * Dao implementation for AppRole.
*/
public class AppRoleDaoImpl extends HibernateGenericDaoImpl<AppRole> implements AppRoleDao {
	
	@Override
	protected void initFilterHqlMap() {
		filterHqlMap.put(BaseObject.FilterHqlName.DEFAULT, "from AppRole s where s.belongTo=? order by s.rolePath");
	}
	
	public List<AppRole> getRolesNotInIds(String roleIds) {
        return this.findByHql("from AppRole vo where vo.roleId not in " + roleIds );
    }
    
    public boolean getIsRoleNameExist(String roleName) {
    	String hql="select count(*) from AppRole where roleName=?";
    	Query query=getSession().createQuery(hql);
    	query.setString(0, roleName);
    	Object obj=query.uniqueResult();
    	Long rows=new Long(obj.toString());
    	if(rows>0)
    		return true;
    	else
    		return false;
    }
    
    public AppRole getByRoleName(String roleName) {
    	AppRole role=null;
    	String hql="from AppRole vo where vo.roleName=?";
    	Object object=findUnique(hql, roleName);
    	if(object!=null){
    		role=(AppRole)object;
    	}else{
    		role=new AppRole();
    	}
    	return role;
    }
    
    public List<AppRole> listRoleTree(Short belongTo){
    	return this.findByHql(filterHqlMap.get(BaseObject.FilterHqlName.DEFAULT), belongTo);
    }
}
